/* * GccApplication2.c * * Created: 2018-05-15 10:33:46 * Author : ine15rsc */ #define F_CPU 8000000UL #define set_bit(PORT,px) (PORT |= _BV(px)) #define clear_bit(PORT,px) (PORT &= ~_BV(px)) #define _BV(bit) (1<<(bit)) #include #include #include #include #include volatile uint16_t count; unsigned int setTemp_enable=0; unsigned int setMoist_enable=0; unsigned int readMoist_enable=0; unsigned int temperature; unsigned int humidity; unsigned int dhc[150]; unsigned char realTemp[4]; unsigned char realHum[4]; unsigned int write_enable=0; unsigned char keytest; unsigned int minTemp; unsigned int minMoist; unsigned char moistVal; unsigned char LCD_clear = 0b00000001; unsigned int keyVal; unsigned int tempVal; unsigned int new_input; unsigned int keyCode; unsigned int dhc_counter= 0; unsigned int parity[6]; unsigned int moistSet; void command_LCD(char cmd){ PORTD |= _BV(PD6); // E hög PORTD &= ~_BV(PD4); // RS låg PORTD &= ~_BV(PD5); // R/W låg PORTB = cmd; _delay_ms(5); PORTD &= ~_BV(PD6); // E låg //_delay_ms(5); PORTD |= _BV(PD6); // E Hög } void writeData(char d){ PORTD |= _BV(PD6); // E hög PORTD |= _BV(PD4); // RS hög PORTD &= ~_BV(PD5); // R/W låg PORTB = d; _delay_ms(10); PORTD &= ~_BV(PD6); // E låg PORTD |= _BV(PD6); // E hög } void write_String(char list[]){ int i = 0; while(list[i] !=NULL){ writeData(list[i]); i++; } } void initDHC(){ MCUCR |= _BV(ISC11); MCUCR &= ~_BV(ISC10); // set up timer with prescaler = 8 TCCR1B |=_BV(CS11); count= 0; } void DHC_run(){ ADCSRA &= ~_BV(ADIE); DDRD |= _BV(PD3); PORTD |= _BV(PD3); PORTD &= ~_BV(PD3); _delay_ms(25); PORTD |= _BV(PD3); DDRD &= ~_BV(PD3); _delay_us(80); TCNT1 = 0; GICR |= _BV(INT1); } void DHC_translate(){ ADCSRA |= 1<38){ dhc_counter=0; DHC_translate(); } write_String("Temperature: "); write_String("C"); command_LCD(0b11000000); write_String("Humidity: "); write_String(" %"); while(1){ startADC(); write_enable=3; if(setMoist_enable==1){ setMoist(); setMoist_enable=0; set_main_screen(); } if(setTemp_enable==1){ setTemp(); setTemp_enable=0; set_main_screen(); } if(readMoist_enable==1){ readMoist(); readMoist_enable=0; set_main_screen(); } itoa(temperature, realTemp,10); command_LCD(0b00001100); command_LCD(0b10001100); write_String(realTemp); _delay_ms(200); command_LCD(0b11001001); itoa(humidity, realHum, 10); write_String(realHum); DHC_run(); if(dhc_counter>38){ dhc_counter=0; DHC_translate(); } command_LCD(0b00001100); if((moistValtemperature)){ DDRD |=_BV(PD7); PORTD |=_BV(PD7); } else{ DDRD &=~_BV(PD7); } } } //ISR - Interrupt service routine ISR(ADC_vect){ int res = ADC; moistVal=res; ADCSRA |= 1<70&&TCNT1<90){ dhc[dhc_counter]=0; TCNT1=0; dhc_counter++; } else if ((TCNT1>90) && (TCNT1 <140)){ TCNT1=0; dhc[dhc_counter]=1; dhc_counter++; } GIFR |= _BV(INTF1); }